﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.Entities.Relations.InferredAssociations
{
    [Serializable]
    public class ClusteringOutput:ResultBase
    {
        #region props
        private int _EntityCount;
        /// <summary></summary>
        public int EntityCount
        {
            get
            {
                return this._EntityCount;
            }
            set
            {
                this._EntityCount = value;
            }
        }

        private int _ClusterCount;
        /// <summary></summary>
        public int ClusterCount
        {
            get
            {
                return this._ClusterCount;
            }
            set
            {
                this._ClusterCount = value;
            }
        }

        private double _Performance;
        /// <summary></summary>
        public double Performance
        {
            get
            {
                return this._Performance;
            }
            set
            {
                this._Performance = value;
            }
        }

        private string _ClusterResultFilePath;
        /// <summary></summary>
        public string ClusterResultFilePath
        {
            get
            {
                return this._ClusterResultFilePath;
            }
            set
            {
                this._ClusterResultFilePath = value;
            }
        }
        #endregion

        #region ctor
        public ClusteringOutput():base(Guid.Empty,Guid.Empty)
        {
            this._EntityCount = 0;
            this._ClusterCount = 0;
            this._Performance = double.NaN;
            this._ClusterResultFilePath = string.Empty;
        }

        public ClusteringOutput(Guid componentID, Guid jobTicketID,
            int entityCount, int clusterCount, double performance, string resultFilePath) 
            : base(componentID, jobTicketID)
        {
            this._EntityCount = entityCount;
            this._ClusterCount = clusterCount;
            this._Performance = performance;
            this._ClusterResultFilePath = resultFilePath;
        }
        #endregion

        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                ClusteringOutput output = new ClusteringOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.EntityCount =
                    int.Parse(XmlDataUtil.GetAttributeValue(xNode, "EntityCount", "0"));
                output.ClusterCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "ClusterCount", "0"));
                output.Performance = double.Parse(XmlDataUtil.GetAttributeValue(xNode, "Performance", "0"));
                output.ClusterResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "ClusterResultFilePath", "");

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "EntityCount", this.EntityCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ClusterCount", this.ClusterCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"Performance",this.Performance.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ClusterResultFilePath", this.ClusterResultFilePath);

            return xNode;
        }
    }
}
